/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "cardsender_srv_proxy.h"
#include "ipc_types.h"
#include "message_option.h"
#include "message_parcel.h"
#include "hilog_wrapper.h"
#include "cardsender_errors.h"
#include "cardsender_common.h"
#include <string.h>

using namespace OHOS;
using namespace std;

namespace SwanLink {
namespace Finance {
int CardsenderSrvProxy::Open(const string& name, const Transport& trans)
{
    CARDSENDER_HILOGE(CARDSENDER_SRV,
                     "open name %{public}s,trans.dev %{public}s",
                     name.c_str(), trans.dev);
    int result = -1;
    sptr<IRemoteObject> remote = Remote();
    if (remote == nullptr) return result;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(CardsenderSrvProxy::GetDescriptor())) {
        CARDSENDER_HILOGE(CARDSENDER_SRV, "Write descriptor failed");
        return result;
    }
    WRITE_PARCEL_WITH_RET(data, String, name, -1);
    if (!data.WriteBuffer(&trans, sizeof(trans))) {
        CARDSENDER_HILOGE(CARDSENDER_SRV, "data.WriteBuffer(&trans,sizeof(trans) fail");
        return ERROR;
    }

    int ret = remote->SendRequest(static_cast<int>(ICardsenderSrv::CARDSENDER_OPEN), data, reply, option);

    if (ret != ERR_OK) {
        CARDSENDER_HILOGE(CARDSENDER_SRV, "SendRequest is failed, ret: %d", ret);
        return result;
    }
    READ_PARCEL_WITH_RET(reply, Int32, result, -1);
    return result;
}

int CardsenderSrvProxy::Close(int handle)
{
    CARDSENDER_HILOGE(CARDSENDER_SRV, "CardsenderSrvProxy::Close");
    int result = -1;
    sptr<IRemoteObject> remote = Remote();
    if (remote == nullptr) return result;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(CardsenderSrvProxy::GetDescriptor())) {
        CARDSENDER_HILOGE(CARDSENDER_SRV, "Write descriptor failed");
        return result;
    }

    WRITE_PARCEL_WITH_RET(data, Int32, handle, E_WRITE_PARCEL_ERROR);

    int ret = remote->SendRequest(static_cast<int>(ICardsenderSrv::CARDSENDER_CLOSE), data, reply, option);

    if (ret != ERR_OK) {
        CARDSENDER_HILOGE(CARDSENDER_SRV, "SendRequest is failed, ret: %d", ret);
        return result;
    }
    READ_PARCEL_WITH_RET(reply, Int32, result, -1);
    return result;
}

int CardsenderSrvProxy::Reset(int handle)
{
    int result = -1;
    sptr<IRemoteObject> remote = Remote();
    if (remote == nullptr) return result;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(CardsenderSrvProxy::GetDescriptor())) {
        CARDSENDER_HILOGE(CARDSENDER_SRV, "Write descriptor failed");
        return result;
    }

    WRITE_PARCEL_WITH_RET(data, Int32, handle, ERROR);

    int ret = remote->SendRequest(static_cast<int>(ICardsenderSrv::CARDSENDER_RESET), data, reply, option);

    if (ret != ERR_OK) {
        CARDSENDER_HILOGE(CARDSENDER_SRV, "SendRequest is failed, ret: %d", ret);
        return result;
    }
    READ_PARCEL_WITH_RET(reply, Int32, result, -1);
    return result;
}

int CardsenderSrvProxy::DispenseCard(int handle)
{
    int result = -1;
    sptr<IRemoteObject> remote = Remote();
    if (remote == nullptr) return result;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(CardsenderSrvProxy::GetDescriptor())) {
        CARDSENDER_HILOGE(CARDSENDER_SRV, "Write descriptor failed");
        return result;
    }

    WRITE_PARCEL_WITH_RET(data, Int32, handle, ERROR);

    int ret = remote->SendRequest(static_cast<int>(ICardsenderSrv::CARDSENDER_DISPENSECARD), data, reply, option);

    if (ret != ERR_OK) {
        CARDSENDER_HILOGE(CARDSENDER_SRV, "SendRequest is failed, ret: %d", ret);
        return result;
    }
    READ_PARCEL_WITH_RET(reply, Int32, result, -1);
    return result;
}

vector<DevAccess> CardsenderSrvProxy::GetSupportAccess()
{
    vector<DevAccess> result;
    sptr<IRemoteObject> remote = Remote();
    if (remote == nullptr) return vector<DevAccess>();
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(CardsenderSrvProxy::GetDescriptor())) {
        CARDSENDER_HILOGE(CARDSENDER_SRV, "Write descriptor failed");
        return vector<DevAccess>();
    }

    int ret = remote->SendRequest(static_cast<int>(ICardsenderSrv::CARDSENDER_GETSUPPORTACCESS), data, reply, option);

    if (ret != ERR_OK) {
        CARDSENDER_HILOGE(CARDSENDER_SRV, "SendRequest is failed, ret: %d", ret);
        return vector<DevAccess>();
    }

    if (!ReadVectorFromParcel(reply, result)) {
        return vector<DevAccess>();
    }

    return result;
}

int CardsenderSrvProxy::QueryStatus(int handle, int *pSuppBinStat, int *pRetainBinStat)
{
    int result = -1;
    sptr<IRemoteObject> remote = Remote();
    if (remote == nullptr) return result;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(CardsenderSrvProxy::GetDescriptor())) {
        CARDSENDER_HILOGE(CARDSENDER_SRV, "Write descriptor failed");
        return result;
    }

    WRITE_PARCEL_WITH_RET(data, Int32, handle, ERROR);
    int ret = remote->SendRequest(static_cast<int>(ICardsenderSrv::CARDSENDER_QUERYSTATUS), data, reply, option);

    if (ret != ERR_OK) {
        CARDSENDER_HILOGE(CARDSENDER_SRV, "SendRequest is failed, ret: %d", ret);
        return result;
    }
    int tmpSuppBinStat = 0;
    int tmpRetainBinStat = 0;
    READ_PARCEL_WITH_RET(reply, Int32, tmpSuppBinStat, -1);
    READ_PARCEL_WITH_RET(reply, Int32, tmpRetainBinStat, -1);
    *pSuppBinStat = tmpSuppBinStat;
    *pRetainBinStat = tmpRetainBinStat;
    READ_PARCEL_WITH_RET(reply, Int32, result, -1);
    // reply.ReadInt32(result);
    CARDSENDER_HILOGI(CARDSENDER_SRV, "CardsenderSrvProxy::QueryStatus result %{public}d", result);
    return result;
}
}  // namespace Finance
}  // namespace SwanLink
